﻿using System;
using System.Collections.Generic;
using System.Text;
using Ninesky.Models;
using Ninesky.InterfaceService;
using System.Threading.Tasks;
using Ninesky.Models.Auxiliary;

namespace Ninesky.Service
{
    public class CategoryService : BaseService<Category>, InterfaceCategoryService
    {
        public CategoryService(NineskyDbContext nineskyDbContext) : base(nineskyDbContext) { }

        public override async Task<OperationResult> AddAsync(Category category)
        {
            OperationResult operationResult = new OperationResult();
            if (category == null)
            {
                operationResult.Succeed = false;
                operationResult.Message = "栏目数据为空";
            }
            else
            {
                switch (category.Type)
                {
                    case CategoryType.General:
                        if (category.CategoryGeneral == null)
                        {
                            operationResult.Succeed = false;
                            operationResult.Message = "常规栏目内容为空";
                        }
                        break;

                }
                await dbContext.Categories.AddAsync(category);
                if (await dbContext.SaveChangesAsync() > 0)
                {
                    operationResult.Succeed = true;
                    operationResult.Message = "添加栏目成功";
                }
                else
                {
                    operationResult.Succeed = false;
                    operationResult.Message = "保存数据失败";
                }
            }
            return operationResult;
        }

    }
}
